/**
 * @class   WeatherSource
 * @brief   Interface for 
 *
 ****************************************************************************
 * @version $Id: WeatherSource.h 2804 2010-07-27 04:39:07Z alex $
 * @author Alessandro Polo
 * @date   Released on 2009-06-04
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_SERVICES_WeatherSource_H__
 #define __WOSH_SERVICES_WeatherSource_H__

 #include <woshDefs.h>
 #include <core/Object.h>
 #include <core/LogContext.h>
 #include <core/Utilities.h>
 #include <core/DataModels.h>
 #include <vector>

 #include <framework/xtended/WeatherInfo.h>


using namespace wosh;
using namespace wosh::xtended;

class WeatherSource : public Object
 {
	WOSH_CLASSNAME(WeatherSource)

	public:
		enum SOURCE_TYPE {
			SOURCE_UNKNOWN	= 0x00,
			SOURCE_SENSOR	= 0x01,
			SOURCE_WEB		= 0x02,
			SOURCE_USER		= 0x04
		 };

	public:
		/**
		 * @brief  Empty constructor.
		 */
		WeatherSource()
			: Object() {
			this->uid = Utilities::randomLong();
			this->uid = _GET_Abs(this->uid);
			Object::setName("WeatherSource", this->uid);
			Log.setContext(Object::getName());
			Log.setIndent( 4 );
			Log.setLevel( LOG_INFO );
			Log.setLevel( LOG_DEBUG );
			clear();
		 }
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~WeatherSource() { }

	public:
		virtual WRESULT updateData( unsigned long timeout = LIMIT_ULONG_MAX, bool* newWeatherAvailable = NULL ) = 0;
		virtual WRESULT loadForecast( WeatherInfo* wCurrentInfo, std::vector<WeatherInfo*>* forecast ) = 0;

		virtual SOURCE_TYPE getSourceType() const = 0;

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		inline int64 getID() const						{ return this->uid; }
		inline bool isEnabled() const					{ return this->enabled; }
		inline long getPriority() const					{ return this->priority; }
		inline double getReliability() const			{ return this->reliability; }
		inline int64 getUpdatePeriod() const			{ return this->updatePeriod; }

		inline int64 getLastUpdated() const				{ return this->lastUpdated; }
		inline WRESULT getLastResult() const			{ return this->lastResult; }

		virtual inline bool shouldBeUpdated() const {
			return ( WFAILED(this->lastResult) || this->lastUpdated + updatePeriod < (int64)Utilities::std_time() );
		 }


		static DataFieldCollector<WeatherSource>* getDataFields() {
			DataFieldCollector<WeatherSource>* fields = new DataFieldCollector<WeatherSource>();
			fields->add<int64, Variant>(&WeatherSource::getID, "ID", 0);
			fields->add<bool, Variant>(&WeatherSource::isEnabled, "Enabled", 1);
			fields->add<long, Variant>(&WeatherSource::getPriority, "Priority", 2);
			fields->add<double, Variant>(&WeatherSource::getReliability, "Reliability", 3);
			fields->add<int64, Variant_TS>(&WeatherSource::getUpdatePeriod, "UpdatePeriod", 4);
			fields->add<int64, Variant_DT>(&WeatherSource::getLastUpdated, "LastUpdated", 5);
			fields->add<WRESULT, Variant>(&WeatherSource::getLastResult, "LastResult", 6);
			fields->add<bool, Variant>(&WeatherSource::shouldBeUpdated, "ShouldBeUpdated", 7);
			return fields;
		 }

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		inline void setEnabled( bool value )			{ this->enabled = value; }
		inline void setPriority( long value )			{ this->priority = value; }
		inline void setReliability( double value )		{ this->reliability = value; }

		virtual void clear() {
			this->enabled = true;
			this->priority = 0;
			this->reliability = 0;
			this->updatePeriod = 360000;
			this->lastUpdated = 0;
			this->lastResult = WRET_OK;
		 }
//@}
	protected:
		virtual void setLastOperation( int64 last_updated = -1, WRESULT last_result = WRET_OK ) {
			if ( last_updated == -1 )
				this->lastUpdated = Utilities::std_time();
			else
				this->lastUpdated = last_updated;
			this->lastResult = last_result;
		 }

		virtual void configureWeatherInfo( WeatherInfo* info, int64 timestamp = -1 ) {
			if ( info == NULL ) return;
			info->setSourceID( this->uid );
			info->setTimestamp(timestamp);
			info->setReliability(this->reliability);
			info->setTTL(this->updatePeriod);
		 }

	protected:
		int64 uid;				///<
		LogContext Log;			///<
		bool enabled;			///<
		long priority;			///<
		double reliability;		///<
		int64 updatePeriod;		///< in milliseconds

		int64 lastUpdated;		///<
		WRESULT lastResult;		///<

}; // class def


#endif //__WOSH_SERVICES_WeatherSource_H__
